/*
 * @(#)com.cloud.food.task 2020/8/13 13:36
 * @Author <a href="mailto:xyqierkang@gmail.com">ErKang Qi</a>
 * @Blog：https://www.qekang.com
 * Copyright (c) 2019-2020 Shanghai
 * All rights reserved.

 * This software is the confidential and proprietary information of
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 */
package com.cloud.lan.pub.task;

import com.cloud.lan.pub.api.dto.weather.WeatherDto;
import com.cloud.lan.pub.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.pub.utils.TalkUtil.dingRobot;

/**
 * 钉钉通知组-天气预报
 * @author ErKang
 * @date Created in 2022/6/1 16:22
 * @title WeatherTask.java
 * Department: Product development
 */
@Component
@Configuration
@Slf4j
@EnableScheduling
public class WeatherTask {

    @Value("${spring.profiles.active}")
    private String env = "dev";

    public static WeatherDto weather(String lat, String lon) throws Exception {
        String host = "https://aliv8.mojicb.com/";
        String path = "/whapi/json/aliweather/condition";
        String method = "POST";
        String appcode = "7892ee74962548148d24c60f532c2ab7";
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("lat", lat);
        bodys.put("lon", lon);
        bodys.put("token", "ff826c205f8f4a59701e64e9e64e01c4");
        /**
         * 重要提示如下:
         * HttpUtils请从
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
         * 下载
         *
         * 相应的依赖请参照
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
         */
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
        //获取response的body
        String result = EntityUtils.toString(response.getEntity());
        Gson gson = new Gson();
        WeatherDto weatherDto = gson.fromJson(result, new TypeToken<WeatherDto>() {
        }.getType());
        return weatherDto;

    }

    public static void main(String[] args) throws Exception {
        WeatherDto sh = weather("31.192438", "121.440114");
        WeatherDto nj = weather("32.066336", "118.76979");
        WeatherDto xy = weather("33.00182457", "118.4905785");
        WeatherDto.DataBean.ConditionBean shCondition = sh.getData().getCondition();
        WeatherDto.DataBean.ConditionBean njCondition = nj.getData().getCondition();
        WeatherDto.DataBean.ConditionBean xyCondition = xy.getData().getCondition();
        String mes = sh.getData().getCity().getName() + "：【" + shCondition.getCondition() + " 】当前湿度：" + shCondition.getHumidity()
                + "° \r \n >体感温度：" + shCondition.getRealFeel() + "° 室外温度：" + shCondition.getTemp() + "°" +
                "\r \n >日出时间：" + shCondition.getSunRise() + "\r \n >日落时间：" + shCondition.getSunSet() +
                " \r \n >紫外线强度：" + shCondition.getUvi() + "° " +
                " 风速：" + shCondition.getWindSpeed() + " 风向：" + shCondition.getWindDir() +
                " \r \n >温馨提示：" + shCondition.getTips() +
                " \r \n >江苏省" + nj.getData().getCity().getName() + "：【" + njCondition.getCondition() + " 】" + "室外温度：" + njCondition.getTemp() + "°" +
                " \r \n >江苏省" + xy.getData().getCity().getName() + "：【" + xyCondition.getCondition() + " 】" + "室外温度：" + xyCondition.getTemp() + "°" +
                " \r \n >以上数据来自：[中国天气](http://www.weather.com.cn/weather/101020100.shtml)";
        dingRobot("心跳测试", mes);
    }

    //每天早上、每隔12小时发送一次天气数据
    @Scheduled(cron = "0 00 10 * * ?")
    private void cancelOrderTask() {
        ThreadUtil.execAsync(() -> {
            try {
                if ("prod".equals(env)) {
                    WeatherDto sh = weather("31.192438", "121.440114");
                    WeatherDto nj = weather("32.066336", "118.76979");
                    WeatherDto xy = weather("33.00182457", "118.4905785");
                    WeatherDto.DataBean.ConditionBean shCondition = sh.getData().getCondition();
                    WeatherDto.DataBean.ConditionBean njCondition = nj.getData().getCondition();
                    WeatherDto.DataBean.ConditionBean xyCondition = xy.getData().getCondition();
                    String mes = sh.getData().getCity().getName() + "：【" + shCondition.getCondition() + " 】当前湿度：" + shCondition.getHumidity()
                            + "° \r \n >体感温度：" + shCondition.getRealFeel() + "° 室外温度：" + shCondition.getTemp() + "°" +
                            "\r \n >日出时间：" + shCondition.getSunRise() + "\r \n >日落时间：" + shCondition.getSunSet() +
                            " \r \n >紫外线强度：" + shCondition.getUvi() + "° " +
                            " 风速：" + shCondition.getWindSpeed() + " 风向：" + shCondition.getWindDir() +
                            " \r \n >温馨提示：" + shCondition.getTips() +
                            " \r \n >江苏省" + nj.getData().getCity().getName() + "：【" + njCondition.getCondition() + " 】" + "室外温度：" + njCondition.getTemp() + "°" +
                            " \r \n >江苏省" + xy.getData().getCity().getName() + "：【" + xyCondition.getCondition() + " 】" + "室外温度：" + xyCondition.getTemp() + "°" +
                            " \r \n >以上数据来自：[中国天气](http://www.weather.com.cn/weather/101020100.shtml)";
                    dingRobot("心跳测试", mes);
                }
            } catch (Exception e) {
                log.info("#异步线程 发送天气 失败 {}", e.getMessage());
            }
        });
    }
}
